//
//  DETabBar.m
//  DemoObjective-CWeb
//
//  Created by Mon on 15/12/13.
//  Copyright © 2015年 Mon. All rights reserved.
//

#import "DETabBar.h"

#define itemCount 5

@implementation DETabBar

- (void)layoutSubviews {
    [super layoutSubviews];
    [self prepareUI];
}

- (void)prepareUI {
    
    CGFloat width = self.bounds.size.width / itemCount;
    CGFloat tabBarH = self.bounds.size.height;
    
    // 设置撰写按钮的frame
    CGRect btnFrame = CGRectMake(0, 0, width, tabBarH);
    
    int index = 0;
    // 遍历tabBar中的子控件
    for (UIView *views in self.subviews) {
        if ([views isKindOfClass:[UIControl class]] && ![views isKindOfClass:[UIButton class]]) {
            views.frame = CGRectOffset(btnFrame, index * width, 0);
            index += index == 1 ? 2 : 1;
        }
    }
    self.composeButton.frame = CGRectOffset(btnFrame, 2 * width, 0);
}

#pragma mark - 懒加载控件
- (UIButton *)composeButton {
    if (!_composeButton) {
        _composeButton = [[UIButton alloc] init];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        // 添加控件到tabBar上面
        [self addSubview:_composeButton];
    }
    return _composeButton;
}

@end
